package com.linedata.acq.metier.norme.fmb;

import oracle.forms.jdapi.Block;
import oracle.forms.jdapi.FormModule;
import oracle.forms.jdapi.Item;
import oracle.forms.jdapi.JdapiIterator;
import oracle.forms.jdapi.Trigger;

public class GTRG_FMB {

	private FormModule	fmb;

	public GTRG_FMB(FormModule fmb) {
		this.fmb = fmb;
	}

	private String	error	= "";

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error += error;
	}

	/**
	 * EKFR016_1 :: Le trigger ne contient pas d'ordre SQL
	 * 
	 * @param fmb
	 */
	public void noSQLinTrigger() {
		// TODO Si un ordre ou un mot clé se trouve dans un commentaire ou
		// message

		// Form Trigger
		for (JdapiIterator triggers = ((FormModule) fmb).getTriggers(); triggers.hasNext();) {
			Trigger trigger = (Trigger) triggers.next();
			if ((trigger.getTriggerText().toUpperCase().contains("INSERT") && trigger.getTriggerText().toUpperCase()
					.contains("FROM"))
					|| (trigger.getTriggerText().toUpperCase().contains("DELETE") && trigger.getTriggerText()
							.toUpperCase().contains("FROM"))
					|| (trigger.getTriggerText().toUpperCase().contains("UPDATE") && trigger.getTriggerText()
							.toUpperCase().contains("FROM"))) {
				this.setError("\tEKFR0016_1 : Form  :: " + trigger.getName() + "\n");
			}
		}

		// Blocks Trigger
		for (JdapiIterator blocks = ((FormModule) fmb).getBlocks(); blocks.hasNext();) {
			Block block = (Block) blocks.next();
			for (JdapiIterator triggers = block.getTriggers(); triggers.hasNext();) {
				Trigger trigger = (Trigger) triggers.next();
				if ((trigger.getTriggerText().toUpperCase().contains("INSERT") && trigger.getTriggerText()
						.toUpperCase().contains("FROM"))
						|| (trigger.getTriggerText().toUpperCase().contains("DELETE") && trigger.getTriggerText()
								.toUpperCase().contains("FROM"))
						|| (trigger.getTriggerText().toUpperCase().contains("UPDATE") && trigger.getTriggerText()
								.toUpperCase().contains("FROM"))) {
					this.setError("\tEKFR0016_1 : Block :: " + block.getName() + "\t:: " + trigger.getName() + "\n");
				}
			}
		}

		// Item Trigger
		for (JdapiIterator blocks = ((FormModule) fmb).getBlocks(); blocks.hasNext();) {
			Block block = (Block) blocks.next();
			for (JdapiIterator items = block.getItems(); items.hasNext();) {
				Item item = (Item) items.next();
				for (JdapiIterator triggers = item.getTriggers(); triggers.hasNext();) {
					Trigger trigger = (Trigger) triggers.next();
					if ((trigger.getTriggerText().toUpperCase().contains("INSERT") && trigger.getTriggerText()
							.toUpperCase().contains("FROM"))
							|| (trigger.getTriggerText().toUpperCase().contains("DELETE") && trigger.getTriggerText()
									.toUpperCase().contains("FROM"))
							|| (trigger.getTriggerText().toUpperCase().contains("UPDATE") && trigger.getTriggerText()
									.toUpperCase().contains("FROM"))) {
						this.setError("\tEKFR0016_1 : Item  :: " + block.getName() + "\t:: " + item.getName() + "\t:: "
								+ trigger.getName() + "\n");
					}
				}
			}
		}
	}

	/**
	 * EKFR022_1 :: Le test sur la globale :global.flag_query = '0' est
	 * effectué dans le trigger WHEN-VALIDATE-ITEM
	 * 
	 * @param fmb
	 */
	public void testGlobal() {

		// Form Trigger
		for (JdapiIterator triggers = ((FormModule) fmb).getTriggers(); triggers.hasNext();) {
			Trigger trigger = (Trigger) triggers.next();
			if ((trigger.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY = '0'")
					|| trigger.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY= '0'") || trigger
					.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY ='0'"))
					&& !trigger.getName().toUpperCase().equals("WHEN-VALIDATE-ITEM")) {
				this.setError("\tEKFR022_1 : Form  :: " + trigger.getName() + "\n");
			}
		}

		// Blocks Trigger
		for (JdapiIterator blocks = ((FormModule) fmb).getBlocks(); blocks.hasNext();) {
			Block block = (Block) blocks.next();
			for (JdapiIterator triggers = block.getTriggers(); triggers.hasNext();) {
				Trigger trigger = (Trigger) triggers.next();
				if ((trigger.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY = '0'")
						|| trigger.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY= '0'") || trigger
						.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY ='0'"))
						&& !trigger.getName().toUpperCase().equals("WHEN-VALIDATE-ITEM")) {
					this.setError("\tEKFR022_1 : Block :: " + block.getName() + "\t:: " + trigger.getName() + "\n");
				}
			}
		}

		// Item Trigger
		for (JdapiIterator blocks = ((FormModule) fmb).getBlocks(); blocks.hasNext();) {
			Block block = (Block) blocks.next();
			for (JdapiIterator items = block.getItems(); items.hasNext();) {
				Item item = (Item) items.next();
				for (JdapiIterator triggers = item.getTriggers(); triggers.hasNext();) {
					Trigger trigger = (Trigger) triggers.next();
					if ((trigger.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY = '0'")
							|| trigger.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY= '0'") || trigger
							.getTriggerText().toUpperCase().contains("GLOBAL.FLAG_QUERY ='0'"))
							&& !trigger.getName().toUpperCase().equals("WHEN-VALIDATE-ITEM")) {
						this.setError("\tEKFR022_1 : Item  :: " + block.getName() + "\t:: " + item.getName() + "\t:: "
								+ trigger.getName() + "\n");
					}
				}
			}
		}
	}

	@Override
	public String toString() {
		return this.getError();
	}
}
